<template>
  <div class="register-page">
    <div class="welcome-text">
      <h1 class="welcome-title">欢迎注册种花吧</h1>
      <p class="slogan">让每一朵花都绽放生命的色彩</p>
    </div>
    <div class="register-container">
      <h2>用户注册</h2>

      <form @submit.prevent="handleSubmit" class="register-form">
        <div class="form-group">
          <i class="el-icon-user"></i>
          <input id="username" v-model="registerData.username" type="text" placeholder="请输入用户名" required />
        </div>
        <div class="form-group">
          <i class="el-icon-lock"></i>
          <input id="password" v-model="registerData.password" type="password" placeholder="请输入密码" required />
        </div>
        <div class="form-group">
          <i class="el-icon-lock"></i>
          <input id="confirmPassword" v-model="confirmPassword" type="password" placeholder="请确认密码" required />
        </div>
        <div class="form-group">
          <i class="el-icon-message"></i>
          <input id="email" v-model="registerData.email" type="email" placeholder="请输入邮箱" required />
          <button @click="sendVerificationCode" type="button" class="code-btn">获取验证码</button>
        </div>
        <div class="form-group">
          <i class="el-icon-key"></i>
          <input id="verificationCode" v-model="verificationCode" type="text" placeholder="请输入验证码" required />
        </div>
        <div class="form-group">
          <i class="el-icon-date"></i>
          <el-date-picker
            v-model="registerData.birthday"
            type="date"
            placeholder="请选择出生日期"
            :picker-options="pickerOptions"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            style="width: 100%"
            @change="calculateAge"
          />
        </div>
        <div class="form-group">
          <i class="el-icon-male"></i>
          <el-radio-group v-model="registerData.gender" class="gender-radio-group">
            <el-radio-button label="male">
              <i class="el-icon-male"></i> 
            </el-radio-button>
            <el-radio-button label="female">
              <i class="el-icon-female"></i> 
            </el-radio-button>
            <el-radio-button label="other">
              <i class="el-icon-user"></i> 
            </el-radio-button>
          </el-radio-group>
        </div>
        <div class="form-group">
          <button type="submit" class="submit-btn">注 册</button>
        </div>
      </form>
      
      <div class="additional-links">
        <router-link to="/login" class="login-link">已有账号？去登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      confirmPassword: '',
      verificationCode: '',
      registerData: {
        username: '',
        password: '',
        email: '',
        birthday: '',
        gender: '',
        age: null
      },
      pickerOptions: {
        disabledDate(time) {
          // 限制选择今天之后的日期
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '十年前',
          onClick(picker) {
            const date = new Date();
            date.setFullYear(date.getFullYear() - 10);
            picker.$emit('pick', date);
          }
        }, {
          text: '二十年前',
          onClick(picker) {
            const date = new Date();
            date.setFullYear(date.getFullYear() - 20);
            picker.$emit('pick', date);
          }
        }]
      }
    };
  },
  methods: {
    sendVerificationCode() {
      if (!this.registerData.email) {
        this.$message.warning('请输入邮箱地址');
        return;
      }
      console.log('发送验证码到邮箱:', this.registerData.email);
      // 这里可以添加发送验证码的逻辑，比如调用API发送验证码
    },
    handleSubmit() {
      if (this.registerData.password !== this.confirmPassword) {
        this.$message.error('两次输入的密码不一致');
        return;
      }
      if (!this.verificationCode) {
        this.$message.warning('请输入验证码');
        return;
      }
      console.log('尝试注册用户:', this.registerData);
      this.request.post("/users/register", this.registerData).then(res => {
        if (res.code === 1) {
          this.$message.success('注册成功');
          this.$router.push('/login');
        } else {
          this.$message.error(res.msg || '注册失败');
        }
      }).catch(err => {
        console.error('注册失败:', err);
        this.$message.error('注册失败，请重试');
      });
    },
    calculateAge(birthday) {
      if (!birthday) return;
      const birthDate = new Date(birthday);
      const today = new Date();
      let age = today.getFullYear() - birthDate.getFullYear();
      const monthDiff = today.getMonth() - birthDate.getMonth();
      
      if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
        age--;
      }
      
      this.registerData.age = age;
      this.$message.success(`您今年${age}岁`);
    }
  }
};
</script>

<style scoped>
.register-page {
  background: linear-gradient(135deg, #e6f3ff 0%, #ffffff 100%);
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10%;
  position: relative;
}

.welcome-text {
  color: #2c3e50;
  max-width: 500px;
  animation: fadeIn 1s ease-out;
}

.welcome-title {
  font-size: 3.5em;
  font-weight: 600;
  margin-bottom: 20px;
  font-family: 'Microsoft YaHei', sans-serif;
  background: linear-gradient(45deg, #3498db, #2980b9);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: titleGlow 2s infinite alternate;
}

.slogan {
  font-size: 1.5em;
  line-height: 1.6;
  color: #3498db;
  font-style: italic;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateX(-50px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes titleGlow {
  from {
    text-shadow: 0 0 10px rgba(52, 152, 219, 0.3);
  }
  to {
    text-shadow: 0 0 20px rgba(52, 152, 219, 0.5);
  }
}

.register-container {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(52, 152, 219, 0.1);
  backdrop-filter: blur(10px);
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    transform: translateX(100px);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
  font-size: 28px;
  font-weight: 500;
}

.register-form {
  margin-top: 20px;
}

.form-group {
  position: relative;
  margin-bottom: 20px;
}

.form-group i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  font-size: 18px;
}

input, select {
  width: 100%;
  padding: 12px 15px 12px 45px;
  border: 1px solid #ddd;
  border-radius: 25px;
  font-size: 16px;
  transition: all 0.3s ease;
  box-sizing: border-box;
  background: rgba(255, 255, 255, 0.9);
}

input:focus, select:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
  outline: none;
}

.code-btn {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  padding: 8px 15px;
  border: none;
  background: #3498db;
  color: white;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.code-btn:hover {
  background: #2980b9;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.submit-btn:hover {
  background: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}

.additional-links {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  padding: 0 10px;
}

.login-link {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.login-link:hover {
  color: #3498db;
}

@media (max-width: 768px) {
  .register-page {
    padding: 20px;
    flex-direction: column;
    justify-content: center;
  }

  .welcome-text {
    text-align: center;
    margin-bottom: 30px;
  }

  .welcome-title {
    font-size: 2.5em;
  }

  .slogan {
    font-size: 1.2em;
  }

  .register-container {
    width: 100%;
    max-width: 400px;
  }
}

.gender-radio-group {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-left: 45px;
}

.gender-radio-group .el-radio-button {
  flex: 1;
  text-align: center;
}

.gender-radio-group .el-radio-button__inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  width: 100%;
}

.gender-radio-group i {
  font-size: 20px;
  margin-bottom: 5px;
}

.el-date-picker {
  width: 100%;
}

.el-date-picker .el-input__inner {
  padding-left: 45px;
}
</style>